///<reference types="jquery" />
/**简单的文件上传功能 */
class UpfileSimple {
  /**上传的文件 */
  public files: any = {};
  /**返回结果 */
  public result = {
    allCount: 0,
    successCount: 0,
    successFiles: [],
    errorCount: 0,
    errorFiles: [],
  };
  /**文件改变触发事件 */
  public fileChange = () => {};

  /**配置 */
  public config = {
    url: "",
    params: {},
    error: (result: any) => {},
    success: (result: any) => {},
    allSuccess: (result: any) => {},
  };
  public getFileCount(): number {
    let length = Object.keys(this.files).length;
    return length;
  }
  public isExistFile(key: string): boolean {
    return this.files.hasOwnProperty(key);
  }
  public addFiles(key: string, file: any) {
    if (!this.files.hasOwnProperty(key)) {
      this.files[key] = file;
      this.result.allCount++;
    }
  }
  public removeFiles(key: string) {
    if (this.files.hasOwnProperty(key)) {
      delete this.files[key];
      this.result.allCount--;
    }
  }
  public upLoadFile() {
    let _this = this;
    if ($.isEmptyObject(this.files)) {
      _this.config.allSuccess(_this.result);
      return;
    }
    for (let key in this.files) {
      var formdata = new FormData();
      for (let param in _this.config.params) {
        formdata.append(param, (_this.config.params as any)[param]);
      }
      formdata.append("file", this.files[key]);
      $.ajax({
        url: this.config.url, //58.16.86.103:8086
        type: "post",
        processData: false,
        dataType: "json",
        contentType: false,
        data: formdata,
        async: true, //异步可能会丢失文件
        success: function (data: any) {
          if (data.code == 0) {
            _this.result.successCount++;
            delete _this.files[key]; //成功删除文件
            (_this.result.successFiles as any).push(data);
            _this.config.success(data);
          } else {
            _this.result.errorCount++;
            (_this.result.errorFiles as any).push(data);
            _this.config.error(data);
          }
          //全部提交
          if (
            _this.result.allCount ==
            _this.result.errorCount + _this.result.successCount
          ) {
            _this.config.allSuccess(_this.result);
            //清空记录
            _this.result.allCount =
              _this.result.allCount - _this.result.successCount;
            _this.result.successCount = 0;
            _this.result.successFiles = [];
            _this.result.errorCount = 0;
            _this.result.errorFiles = [];
          }
        },
        error: function (window: any, success: any, errorvale: any) {
          _this.result.errorCount++;
          (_this.result.errorFiles as any).push(errorvale);
          _this.config.error(errorvale);
          //全部提交
          if (
            _this.result.allCount ==
            _this.result.errorCount + _this.result.successCount
          ) {
            _this.config.allSuccess(_this.result);
            //清空记录
            _this.result.allCount =
              _this.result.allCount - _this.result.successCount;
            _this.result.successCount = 0;
            _this.result.successFiles = [];
            _this.result.errorCount = 0;
            _this.result.errorFiles = [];
          }
        },
      });
    }
  }
}
